package com.weixing.mall.provider.controller.admin;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoNeedAccess;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.ProductStockDetails;
import com.weixing.mall.provider.model.query.ProductStockQuery;
import com.weixing.mall.provider.model.vo.ProductStockListVo;
import com.weixing.mall.provider.service.IProductStockDetailsService;
import com.weixing.mall.provider.service.IProductStockService;
import com.weixing.mall.util.ListUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Small
 * @since 2021-07-30
 */
@RestController
@RequestMapping("/productStockDetails")
public class ProductStockDetailsController extends BaseController {

    @Autowired
    private IProductStockService productStockService;
    @Autowired
    private IProductStockDetailsService productStockDetailsService;


    /**
     * 二期项目已经找到替代办法，
     *
     * @return
     */
    @ApiOperation(value = "库存管理列表")
    @GetMapping("/init")
    @NoNeedAccess
    public Result selectOfPage() {
        ProductStockQuery query = new ProductStockQuery();
        List<ProductStockListVo> productStockList = productStockService.selectList(query);
        List<List<ProductStockListVo>> tmpList = ListUtil.fixedGrouping(productStockList, 100);

        for (List<ProductStockListVo> productStockListVos : tmpList) {
            List<ProductStockDetails> productStockDetailList = Lists.newArrayList();
            for (ProductStockListVo productStockListVo : productStockListVos) {
                Integer stock = productStockListVo.getStock();
                for (int i = 0; i < stock; i++) {
                    ProductStockDetails productStockDetails = new ProductStockDetails();
                    BeanUtils.copyProperties(productStockListVo, productStockDetails);
                    productStockDetails.setStockPrice(new BigDecimal(productStockListVo.getDiscountPrice()));
                    productStockDetails.setMarkerPrice(new BigDecimal(productStockListVo.getMarketPrice()));
                    productStockDetailList.add(productStockDetails);

                }
            }
            productStockDetailsService.saveBatch(productStockDetailList);
        }
        return ResultUtil.success();
    }
    /**
     * 二期项目已经找到替代办法，
     *
     * @return
     */
    @ApiOperation(value = "库存管理列表")
    @GetMapping("/updateStock/{id}/{stockSn}")
    @NoNeedAccess
    public Result updateStock(@PathVariable Long id , @PathVariable String stockSn) {
        try{
            ProductStockDetails details=productStockDetailsService.getOne(new QueryWrapper<ProductStockDetails>().eq("stock_sn",stockSn));
            if(ObjectUtil.isNotNull(details)&&!details.getId().equals(id)){
                return  ResultUtil.error("库存编码重复，不允许提交！");
            }
        }catch (Exception e){
            return  ResultUtil.error("库存编码重复，不允许提交！");
        }

        ProductStockDetails details=new ProductStockDetails();
        details.setId(id);
        details.setStockSn(stockSn);
        productStockDetailsService.updateById(details);
        return ResultUtil.success();
    }
}
